import React from 'react';

import style from './index.module.css';

const wrapper = (fn) => {
  return (...args) => {
    console.log('myConnect wrapper', fn, args);
    return fn(...args);
  };
};

const functionWrapper = (MapDispatchToPropsParam) => {
  let newMapDispatchToPropsParam = {};
  if (MapDispatchToPropsParam && typeof MapDispatchToPropsParam === 'object') {
    for (let key in MapDispatchToPropsParam) {
      newMapDispatchToPropsParam[key] = wrapper(MapDispatchToPropsParam[key]);
    }
  }
  return newMapDispatchToPropsParam;
};

/**
 *
 * @param {*} MapStateToPropsParam 回调函数，有返回值
 * @param {*} MapDispatchToPropsParam 包装函数对象
 * @returns
 */
const myConnect = (MapStateToPropsParam, MapDispatchToPropsParam) => {
  let state = { type: 1 };
  let mapStateToPropsParams = MapStateToPropsParam(state);

  // 新增处理逻辑
  let mapDispatchToPropsParams = functionWrapper(MapDispatchToPropsParam);
  return (Component) => {
    return (props) => {
      return (
        <div className={style.index}>
          <Component
            {...mapStateToPropsParams}
            {...mapDispatchToPropsParams}
            {...props}
          />
        </div>
      );
    };
  };
};

export { myConnect };
